package com.managementSystem.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.managementSystem.client.StaffBeanServiceAsync;
import com.managementSystem.client.event.AuthenticateStaff;
import com.managementSystem.client.event.AuthenticateStaffEventHandler;
import com.managementSystem.client.event.MainMenu;
import com.managementSystem.client.event.MainMenuEventHandler;
import com.managementSystem.client.event.RegisterStaff;
import com.managementSystem.client.event.RegisterStaffEventHandler;
import com.managementSystem.client.presenter.LoginPresenter;
import com.managementSystem.client.presenter.MainMenuPresenter;
import com.managementSystem.client.presenter.Presenter;
import com.managementSystem.client.presenter.RegisterPresenter;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.managementSystem.client.view.LoginView;
import com.managementSystem.client.view.MainMenuView;
import com.managementSystem.client.view.RegisterView;

public class AppController implements Presenter, ValueChangeHandler<String> {
	private final HandlerManager eventBus;
	private final StaffBeanServiceAsync rpcStaffService; 
	private final RegisterBeanServiceAsync rpcRegisterService; 
	private HasWidgets container;

	public AppController(StaffBeanServiceAsync rpcStaffService, RegisterBeanServiceAsync rpcRegisterService, HandlerManager eventBus) {
		this.eventBus = eventBus;
		this.rpcStaffService = rpcStaffService;
		this.rpcRegisterService = rpcRegisterService;
		bind();
	}
	private void bind() {
		History.addValueChangeHandler(this);

		eventBus.addHandler(AuthenticateStaff.TYPE,
				new AuthenticateStaffEventHandler() {
			public void onLogin(AuthenticateStaff event) {
				doAuthenticateStaff();
			}
		});
		
		eventBus.addHandler(RegisterStaff.TYPE,
				new RegisterStaffEventHandler() {
			public void onRegister(RegisterStaff event) {
				doRegister();
			}	
		});
		
		eventBus.addHandler(MainMenu.TYPE,
				new MainMenuEventHandler() {
			public void onMainMenu(MainMenu event) {
				doMainMenu();
			}	
		});
	}
	
	
	
	private void doAuthenticateStaff() {
		History.newItem("login");
	}
	
	private void doMainMenu() {
		History.newItem("menu");
		
	}
	private void doRegister() {
		History.newItem("register");
	}
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token != null) {
			Presenter presenter = null;
			if (token.equals("login")) {
				presenter = new LoginPresenter(rpcStaffService, eventBus, new LoginView());
			} else if (token.equals("register")) {
				presenter = new RegisterPresenter(rpcRegisterService,rpcStaffService, eventBus, new RegisterView());
			} else if (token.equals("menu")) {
				presenter = new MainMenuPresenter(rpcStaffService, eventBus, new MainMenuView());
			} if (presenter != null) {
				presenter.go(container);
			}
		}
	}
	@Override
	public void go(HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem("login");
		}
		else {
			History.fireCurrentHistoryState();
		}
	}

}
